// 引入pinia
import { defineStore } from "pinia"
import type { CategoryStateModel } from "@/api/product/model/categoryModel"
// 引入接口函数
import {
  reqGetCategory1ListData,
  reqGetCategory2ListData,
  reqGetCategory3ListData,
} from "@/api/product/category"
// 创建模块对象
export const useCategoryListStore = defineStore("categoryList", {
  state: (): CategoryStateModel => {
    return {
      // 以下三个属性用于收集用户真正选择的分类的id
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

      // 以下三个数组用于存储服务器响应回来的分类数据
      category1List: [],
      category2List: [],
      category3List: [],
    }
  },
  actions: {
    // 获取一级
    async getCategory1ListData() {
      const result = await reqGetCategory1ListData()
      // console.log("一级分类", result)
      this.category1List = result
    },
    // 获取二级
    async getCategory2ListData(id: number) {
      this.category1Id = id
      const result = await reqGetCategory2ListData(id)
      // console.log("二级分类", result)
      this.category2List = result

      // 重置二三级ID
      this.category2Id = undefined
      this.category3Id = undefined
    },
    // 获取三级
    async getCategory3ListData(id: number) {
      this.category2Id = id
      const result = await reqGetCategory3ListData(id)
      // console.log("三级分类", result)
      this.category3List = result

      // 重置三级id
      this.category3Id = undefined
    },
    // 设置三级分类ID
    setCategory3Id(id: number) {
      this.category3Id = id
    },
  },
})
